const jd = require('../mng/jd')
const logger = require('log4js').getLogger()

var lastQueryOrderListTime = new Date('2020/07/07 23:00:00').getTime()

exports.start = () => {
    var date = new Date()
    var strLatestQueryOrderListTime = date.getFullYear() +
        (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1) +
        (date.getDate() < 10 ? '0' : '') + date.getDate() +
        (date.getHours() < 10 ? '0' : '') + date.getHours()

    var date = new Date(lastQueryOrderListTime)
    var strLastQueryOrderListTime = date.getFullYear() +
        (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1) +
        (date.getDate() < 10 ? '0' : '') + date.getDate() +
        (date.getHours() < 10 ? '0' : '') + date.getHours()

    const mongoMng = require('../mng/mongoMng')
    if (!mongoMng.db) {
        logger.error('[' + __filename + ':' + __line + ']', 'mongodb未连接')
        return
    }
    logger.debug('[' + __filename + ':' + __line + ']', 'get jd bill ' + strLastQueryOrderListTime)
    jd.union.open.order.query(strLastQueryOrderListTime).then(result => {
        result.forEach(item => {
            mongoMng.db.collection('bill').find({
                orderId: item.orderId
            }).toArray((err, result) => {
                if (err) {
                    logger.error('[' + __filename + ':' + __line + ']', err)
                    return
                }
                if (result.length == 0) {
                    if (item.splitName == '已拆单' ||
                        item.validCodeStr == '无效-拆单' ||
                        item.validCodeStr == '无效-取消' ||
                        item.validCodeStr == '暂停' ||
                        item.validCode == 2) {
                        return
                    }
                    mongoMng.db.collection('bill').insert([item], (err, result) => {
                        if (err) {
                            logger.error('[' + __filename + ':' + __line + ']', err)
                            return
                        }
                        require('./handleBillTask').findCustomerForBill(item).then(result => {
                            var str = '有新京东订单[' + item.orderId + ',' + result + ']'
                            logger.debug('[' + __filename + ':' + __line + ']', str)
                            require('../mng/pushMng').push(str)
                        }, result => {
                            var str = '有新京东订单[' + item.orderId + ']'
                            logger.debug('[' + __filename + ':' + __line + ']', str)
                            require('../mng/pushMng').push(str)
                        })
                    })
                } else {
                    if (result[0].orderStatus != item.orderStatus && result[0].validCode != item.validCode) {
                        var str = '京东订单[' + item.orderId + ']状态从' + result[0].orderStatus + '、' + result[0].validCode + '变为' + item.orderStatus + '、' + item.validCode
                        logger.debug('[' + __filename + ':' + __line + ']', str)
                        require('../mng/pushMng').push(str)
                    }
                    mongoMng.db.collection('bill').update({
                        orderId: item.orderId
                    }, {
                        $set: item
                    }, (err, result) => {
                        if (err) {
                            logger.error('[' + __filename + ':' + __line + ']', err)
                            return
                        }
                    })
                }
            })
        })
        if (strLastQueryOrderListTime != strLatestQueryOrderListTime) {
            lastQueryOrderListTime += 3600000
            exports.start()
        }
    })
}